import openpyxl
from parseSql import get_table_name
import os

# 定义写入路径
write_path='C:\\Users\\me\\Downloads\\指标梳理-YDM.xlsx'
#写入execl执行得行和列
wr=openpyxl.load_workbook(write_path)
sheet =wr['Sheet1']

#读取脚本文件或者目录
# read_file_dir='C:\\ruanjian\\svn_newhope\\ETL\\programs(生产环境)\\fdc\\fdc_dc\\dws\\dws_cust_adviserIdentifySum.sql'
read_file_dir='C:\\Users\\me\\Desktop\\工作日志\\大数据平台\\华为poc\\脚本1\\完成'


files=set()
if os.path.isdir(read_file_dir):
    files=os.listdir(read_file_dir)
elif os.path.isfile(read_file_dir):
    files.add(read_file_dir)
i=0
end_line=0
for file in files:
    if file.lower().find('ddl')>=0 :
        continue
    if os.path.isdir(read_file_dir):
        file_path= read_file_dir+'\\'+file
    else:
        file_path=file    
    table_name_set=get_table_name(sqlfile=file_path)
    names_set=set()
    target_name= 'fdc_ads.{}'.format(file.split('.sql')[0]).lower()
    for name in table_name_set:
        name=str(name).lower()
        if target_name==name:
            continue
        names_set.add(name)
        pass
    cnt=0
    for name in names_set:
        # 写入依赖表
        sheet.cell(row=i+1,column=2,value=name)
        # 写入目标表
        sheet.cell(row=i+1,column=1,value=target_name)
        i=i+1
        cnt=cnt+1
    # sheet.merge_cells(start_row=i-cnt+1,start_column=1,end_column=1,end_row=i)
    wr.save(write_path)

